﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RTSoft.SoureCode.Control.XuatXu;
using RTSoft.Modules;

namespace RTSoft.SoureCode.Entity
{
    public partial class frmXuatXu : DevExpress.XtraEditors.XtraForm
    {
        public frmXuatXu()
        {
            InitializeComponent();
        }
        DataTable tbl;
        String errConnect;
        String errSQL;
        private void frmXuatXu_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitGrid();
            }
            catch
            {
                Library.ShowMessage("Lỗi dữ liệu đầu vào!");
            }
        }

        public void InitGrid()
        {
            tbl = new DataTable();
            errConnect = "";
            errSQL = "";
            xuatxuctrl ctrl = new xuatxuctrl();
            ctrl.getData(ref errConnect, ref errSQL, ref tbl);
            GridControl1.DataSource = tbl;
            GridView1.Columns["ID_XX"].Caption = "ID";
            GridView1.Columns["TENNUOC"].Caption = "Tên Nước";           
            GridView1.Columns["ID_XX"].Visible = false;
            GridView1.BestFitColumns();
        }
        #region "Event Button"
        private void btnExcel_Click(object sender, EventArgs e)
        {
            Library.ExportToExcel(this, "Xuất Xứ", GridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (GridView1.FocusedRowHandle >= 0)
                {
                    int id = Convert.ToInt32(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_XX"].FieldName));
                    errConnect = "";
                    errSQL = "";
                    string strName = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["TENNUOC"].FieldName).ToString();
                    Boolean result = Library.ShowQuestionMessage("Bạn có chắc chắn xóa" + strName + " ?");
                    if (result == false)
                    {
                        return;
                    }
                    xuatxuctrl ctrl = new xuatxuctrl();
                    ctrl.Delete(ref errConnect, ref errSQL, id);
                    if (errSQL != "")
                    {
                        Library.ShowMessage("Không thể xóa quốc gia này");
                    }
                    else
                    {
                        GridView1.DeleteSelectedRows();
                        GridView1.RefreshData();
                    }
                }
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                xuatxuctrl ctrl = new xuatxuctrl();
                xuatxuinfo info = new xuatxuinfo();

                errConnect = "";
                errSQL = "";
                DataRow row = GridView1.GetDataRow(e.RowHandle);
                info.ID_XX = Convert.ToInt32(RTValidate.CheckNull(row["ID_XX"]));
                info.TENNUOC =RTValidate.EmptyNull(row["TENNUOC"]).ToString();
                if (info.ID_XX != 0)
                {
                    Boolean result = Library.ShowQuestionMessage("Dữ liệu có sự thay đổi bạn có chắc muốn sửa?");
                    if (result != false)
                    {
                        ctrl.Update(ref errConnect, ref errSQL, info);
                    }
                }
                else
                {
                    ctrl.Insert(ref errConnect, ref errSQL, info);
                }
                this.InitGrid();
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

      
    }
}